/**
 * 闯关数据
 *
 * created by qiuwei
 * 2016年05月17日
 */
'use strict';

const builder = require('../filterBuilder');
const api = require('../dicAPIUrl');
const fillToMapHandler = require('../util').fillToMapHandler;
const fillMultiDataToMapHandler = require('../util').fillMultiDataToMapHandler;

/**
 * @public
 * 解析请求数据
 *
 * @param (object) requestData 请求数据
 * @return 请求参数
 */
function request(requestData) {
	let filters = filter(requestData.condition);
	let timeUnit = requestData.currentTimeUnit;

    // 新关卡 - 闯关人数
    let newMissionPeopleCount = {
        "data": {
            "target": "hurdle_record-student_id",
            "data_type": "count",
            "type": "normal"
        },
        "time_unit": timeUnit,
        "multi_unit": "",
        "filter": [{
            "item": "hurdle_record-play_type",
            "type": "=",
            "value": 1 //#1：闯关；2：PK
        },{
            "item": "hurdle_record-new_hurdle",
            "type": "=",
            "value": 1 // #是否是新闯关，只对闯关有意义。1：新关卡；0：非新关卡'
        }].concat(filters)
    };

    // 新关卡 - 闯关次数
    let newMissionTimeCount = {
        "data": {
            "target": "hurdle_record-hurdle_record_id",
            "data_type": "count",
            "type": "normal"
        },
        "time_unit": timeUnit,
        "multi_unit": "",
        "filter": [{
            "item": "hurdle_record-play_type",
            "type": "=",
            "value": 1 // #1：闯关；2：PK
        },{
            "item": "hurdle_record-new_hurdle",
            "type": "=",
            "value": 1 // #是否是新闯关，只对闯关有意义。1：新关卡；0：非新关卡'
        }].concat(filters)
    };

    // 新关卡- 成功闯关次数
    let newMissionEndCount = {
        "data": {
            "target": "hurdle_record-hurdle_record_id",
            "data_type": "count",
            "type": "normal"
        },
        "time_unit": timeUnit,
        "multi_unit": "",
        "filter": [{
            "item": "hurdle_record-play_type",
            "type": "=",
            "value": 1 // #1：闯关；2：PK
        },{
            "item": "hurdle_record-new_hurdle",
            "type": "=",
            "value": 1 // #是否是新闯关，只对闯关有意义。1：新关卡；0：非新关卡'
        },{
            "item": "hurdle_record-play_result",
            "type": "=",
            "value": 1 // #闯关或PK结果。1：成功；2：失败；3：放弃
        }].concat(filters)
    };

    // 新关卡 - 关卡数
    let newMissionCount = {
        "data": {
            "target": "hurdle_record-hurdle_id",
            "data_type": "count",
            "type": "normal"
        },
        "time_unit": timeUnit,
        "multi_unit": "hurdle_record-student_id",
        "filter": [{
            "item": "hurdle_record-play_type",
            "type": "=",
            "value": 1 // #1：闯关；2：PK
        },{
            "item": "hurdle_record-new_hurdle",
            "type": "=",
            "value": 1 // #是否是新闯关，只对闯关有意义。1：新关卡；0：非新关卡'
        }].concat(filters)
    };

    // 全部闯关 - 人数
    let allMissionPeopleCount = {
        "data": {
            "target": "hurdle_record-student_id",
            "data_type": "count",
            "type": "normal"
        },
        "time_unit": timeUnit,
        "multi_unit": "",
        "filter": [{
            "item": "hurdle_record-play_type",
            "type": "=",
            "value": 1// #1：闯关；2：PK
        }].concat(filters)
    };

    // 全部闯关 - 次数
    let allMissionTimeCount = {
        "data": {
            "target": "hurdle_record-hurdle_record_id",
            "data_type": "count",
            "type": "normal"
        },
        "time_unit": timeUnit,
        "multi_unit": "",
        "filter": [{
            "item": "hurdle_record-play_type",
            "type": "=",
            "value": 1 //  #1：闯关；2：PK
        }].concat(filters)
    };

    // 全部闯关 - 成功闯关次数
    let allMissionEndCount = {
        "data": {
            "target": "hurdle_record-hurdle_record_id",
            "data_type": "count",
            "type": "normal"
        },
        "time_unit": timeUnit,
        "multi_unit": "",
        "filter": [{
            "item": "hurdle_record-play_type",
            "type": "=",
            "value": 1 //  #1：闯关；2：PK
        },{
            "item": "hurdle_record-play_result",
            "type": "=",
            "value": 1 // #闯关或PK结果。1：成功；2：失败；3：放弃
        }].concat(filters)
    };

    // 全部闯关 - 关卡数
    let allMissionCount = {
        "data": {
            "target": "hurdle_record-hurdle_id",
            "data_type": "count",
            "type": "normal"
        },
        "time_unit": timeUnit,
        "multi_unit": "hurdle_record-student_id",
        "filter": [{
            "item": "hurdle_record-play_type",
            "type": "=",
            "value": 1 //  #1：闯关；2：PK
        }].concat(filters)
    };

    return [
        newMissionPeopleCount, newMissionTimeCount, newMissionEndCount, newMissionCount,
        allMissionPeopleCount, allMissionTimeCount, allMissionEndCount, allMissionCount
    ] ;
}

/**
 * @public
 * 解析响应数据
 */
function response(newMissionPeopleCount, newMissionTimeCount, newMissionEndCount, newMissionCount,
                  allMissionPeopleCount, allMissionTimeCount, allMissionEndCount, allMissionCount) {

    let set = new Set();
    let handler = fillToMapHandler.bind(set);

    let newMissionPeopleCountMap = handler(newMissionPeopleCount);
    let newMissionTimeCountMap = handler(newMissionTimeCount);
    let newMissionEndCountMap = handler(newMissionEndCount);
    let newMissionCountMap = fillMultiDataToMapHandler(newMissionCount);
    // let newMissionCountMap = handler(newMissionCount);

    let allMissionPeopleCountMap = handler(allMissionPeopleCount);
    let allMissionTimeCountMap = handler(allMissionTimeCount);
    let allMissionEndCountMap = handler(allMissionEndCount);
    let allMissionCountMap = fillMultiDataToMapHandler(allMissionCount);
    // let allMissionCountMap = handler(newMissionCount);

    let rn = {
        categories: Array.from(set),
        newMissionPeopleCountSeries: [],
        newMissionTimeCountSeries: [],
        newMissionEndCountSeries: [],
        newMissionCountSeries: [],
        allMissionPeopleCountSeries: [],
        allMissionTimeCountSeries: [],
        allMissionEndCountSeries: [],
        allMissionCountSeries: []
    };

    rn.categories.forEach((item, index) => {
        rn.newMissionPeopleCountSeries.push(+newMissionPeopleCountMap[item] || 0);
        rn.newMissionTimeCountSeries.push(+newMissionTimeCountMap[item] || 0);
        rn.newMissionEndCountSeries.push(+newMissionEndCountMap[item] || 0);
        rn.newMissionCountSeries.push(+newMissionCountMap[item] || 0);

        rn.allMissionPeopleCountSeries.push(+allMissionPeopleCountMap[item] || 0);
        rn.allMissionTimeCountSeries.push(+allMissionTimeCountMap[item] || 0);
        rn.allMissionEndCountSeries.push(+allMissionEndCountMap[item] || 0);
        rn.allMissionCountSeries.push(+allMissionCountMap[item] || 0);
    });
    console.log(rn.categories);
    return rn;
}

/**
 * @private
 * 解析过滤条件
 *
 * @param (object) condition 请求数据中的condition字段
 * @return 过滤条件数组
 */
function filter(condition) {
    let filters = [];

    if (condition.province && condition.province != builder.NIL) {
        filters.push(builder.append('student-province_id', '=', condition.province));
    }
    if (condition.city && condition.city != builder.NIL) {
        filters.push(builder.append('student-city_id', '=', condition.city));
    }
    if (condition.county && condition.county != builder.NIL) {
        filters.push(builder.append('student-county_id', '=', condition.county));
    }
    if (condition.school && condition.school != builder.NIL) {
        filters.push(builder.append('student-school_id', '=', condition.school));
    }
    if (condition.stage[0] != builder.NIL) {
        filters.push(builder.append('student-grade_part', 'in', condition.stage.join(',')));
    }
    if (condition.grade[0] != builder.NIL) {
        filters.push(builder.append('student-grade', 'in', condition.grade.join(',')));
    }
    if (condition.subject[0] != builder.NIL) {
        filters.push(builder.append('hurdle_student_ext-hurdle_subject', 'in', condition.subject.join(',')));
    }
    if (condition.version != '0') {
        filters.push(builder.append('student-version', '=', condition.version));
    }
    if (condition.deviceType[0] != builder.NIL) {
        filters.push(builder.append('student-source', 'in', condition.deviceType.join(',')));
    }

    filters.push(builder.append('hurdle_record-day', '>=', condition.startDate));
    filters.push(builder.append('hurdle_record-day', '<=', condition.endDate));
    filters.push(builder.append('student-teacher_student_count', '>=', condition.userFeature));

    return filters;

}

module.exports = {
    API_URL: api.API_PACKAGE_DATA,
    request,
    response
}
